package cn.com.controller;

import cn.com.beans.*;
import cn.com.service.BatchLineService;
import cn.com.service.CollegeInfoService;
import cn.com.service.FractionInfoService;
import cn.com.service.ScoreLineService;
import cn.com.util.Arithmetic;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/insertLines")
public class InsertLinesController {
    @Resource
    private CollegeInfoService collegeInfoService;
    @Resource
    private FractionInfoService fractionInfoService;
    @Resource
    private BatchLineService batchLineService;
    @Resource
    private ScoreLineService scoreLineService;

    @GetMapping
    public String home(){
        return "Insert/InsertLines";
    }

    @GetMapping("/inquireSchool/{coNameValue}")
    @ResponseBody
    public List<VipActivateReturn> inquireSchool(@PathVariable String coNameValue){
        CollegeInfo collegeInfo = new CollegeInfo();
        collegeInfo.setCoName(coNameValue);
        List<CollegeInfo> activationCodes = collegeInfoService.findAll(collegeInfo);
        VipActivateReturn vipActivateReturn = new VipActivateReturn();
        if(activationCodes.size() == 0){
            vipActivateReturn.setMsg("校名输入错误");
            vipActivateReturn.setType(0);
        }else{
            vipActivateReturn.setMsg("校名输入正确");
            vipActivateReturn.setType(1);
        }
        List<VipActivateReturn> list = new ArrayList<>();
        list.add(vipActivateReturn);
        return list;
    }

    @PostMapping("/insert")
    @ResponseBody
    public Map<String,Object> insertLine(HttpServletRequest request){
        Map<String,Object> result = new HashMap<>();
        String schoolName = request.getParameter("coName");
        Integer frType = Integer.valueOf(request.getParameter("frType"));
        String type;
        if(frType == 0){
            type = "文科";
        }else{
            type = "理科";
        }
        CollegeInfo collegeInfo = new CollegeInfo();
        collegeInfo.setCoName(schoolName);
        List<CollegeInfo> activationCodes = collegeInfoService.findAll(collegeInfo);
        Integer coId = activationCodes.get(0).getCoId();
        String coBatch = activationCodes.get(0).getCoBatch();
        YearGrade yearGrade1 = new YearGrade();
        Integer grade1 = Integer.valueOf(request.getParameter("2015"));
        yearGrade1.setGrade(grade1);
        yearGrade1.setYear(2015);
        YearGrade yearGrade2 = new YearGrade();
        Integer grade2 = Integer.valueOf(request.getParameter("2016"));
        yearGrade2.setGrade(grade2);
        yearGrade2.setYear(2016);
        YearGrade yearGrade3 = new YearGrade();
        Integer grade3 = Integer.valueOf(request.getParameter("2017"));
        yearGrade3.setGrade(grade3);
        yearGrade3.setYear(2017);
        YearGrade yearGrade4 = new YearGrade();
        Integer grade4 = Integer.valueOf(request.getParameter("2018"));
        yearGrade4.setGrade(grade4);
        yearGrade4.setYear(2018);
        YearGrade yearGrade5 = new YearGrade();
        Integer grade5 = Integer.valueOf(request.getParameter("2019"));
        yearGrade5.setGrade(grade5);
        yearGrade5.setYear(2019);
        List<YearGrade> list = new ArrayList<>();
        list.add(yearGrade1);
        list.add(yearGrade2);
        list.add(yearGrade3);
        list.add(yearGrade4);
        list.add(yearGrade5);
        FractionInfo fractionInfo = new FractionInfo();
        for(YearGrade yg : list){
            fractionInfo.setFrCoid(coId);
            fractionInfo.setFrType(frType);
            fractionInfo.setFrYear(yg.getYear());
            FractionInfo info = fractionInfoService.findAll(fractionInfo);
            if(info != null){
                result.put("code",0);
                result.put("msg","有数据已经存在,添加失败");
                return result;
            }
        }
        for(YearGrade yg : list){
            fractionInfo.setFrCoid(coId);
            fractionInfo.setFrScore(yg.getGrade());
            fractionInfo.setFrType(frType);
            fractionInfo.setFrYear(yg.getYear());
            fractionInfoService.insert(fractionInfo);
        }
        result.put("code",0);
        result.put("msg","添加成功");
        return result;
    }
}
